<?php
class OtherAction extends AdminPublicAction{

/*======================================================================
	信息列表
======================================================================================*/
	public function index(){
		
		if((isset($_GET['sort'] ) && !empty($_GET['sort'])) && (isset($_GET['updown'] ) && !empty($_GET['updown']))){
			 $sort   = I('sort');
			 $upDown = I('updown');
		}else{
			 $_GET['sort'] = $sort = "other_id";
			 $_GET['updown'] = $upDown = "desc";
			
		}
		
		$keyword = I('keyword','');
		
		//關鍵字查詢
		if(tep_not_null($keyword)){
			$where['name']  = array('like','%'.$keyword.'%');
			$where['tel']  = array('like','%'.$keyword.'%');
			$where['_logic'] = 'or';
			$map['_complex'] = $where;
		}
		$begintime = I('begintime','');
		$endtime = I('endtime','');
		
		//echo $begintime;die;
		
		if($begintime){
			$this->begintime = $begintime;
			$begintime = date('Y-m-d H:i:s',strtotime($begintime));
			$map['add_date'] = array('egt',$begintime);			
			if($endtime){
				$endtime = date('Y-m-d H:i:s',strtotime($endtime));
				$map["add_date"] = array(array('EGT',$begintime),array('ELT',$endtime),'AND');
			}else{
				$endtime = date('Y-m-d H:i:s',time());
				$map["add_date"] = array(array('EGT',$begintime),array('ELT',$endtime),'AND');
			}	
			//echo $endtime;die;		
			$this->endtime = date('Y-m-d',strtotime($endtime));
		}elseif($endtime){			
				//$this->endtime = $endtime;
				$endtime = date('Y-m-d H:i:s',strtotime($endtime));
				$map['add_date'] = array('elt',$endtime);
				$this->endtime = date('Y-m-d',$endtime);
		}
		
			
		//$map["add_date"] = array(array('EGT',$begintime),array('ELT',$endtime),'AND');
		//省份城市筛选
		$qid = I('citycode','');
		$aid = I('area_id','');
		
		if($qid > 0){
			$map['cityCode'] = $qid;
		}
		// 社区筛选
		if ($aid > 0) {
			$map['areaCode'] = $aid;
		}
		$map = get_map_city_id($map);
		//var_dump($map);die;
		$this->citycode = $map['cityCode'];
		$this->area_id = $map['areaCode'];
		
		import("ORG.Util.Page");
		$page = 10;		
		$count = D('OtherView')->where($map)->count();	
		$Page = new Page($count, $page);	
		$infoList = D('OtherView')->getAll($map,$Page->firstRow. ',' . $Page->listRows,$sort.' '.$upDown);	
		//var_dump($infoList);die;
		if (S("success") != "")
			{
				$this->assign("msg", '<div class="success">'.S("success").'</div>');
				S('success',NULL);
			}
			
			if (S("err") != "")
			{
				$this->assign("msg", '<div class="err">'.S("err").'</div>');
				S('err',NULL);
			}

		/* echo "<pre>";
		var_dump($infoList);die; */
		$this->keyword = $keyword;
		$this->page = $Page->show();	
		$this->infoList = $infoList;
		$this->li1 = 'current';
		$this->left_other = 'current';
		$this->display();
	}

/*======================================================================
	信息刪除
======================================================================================*/
	public function delete(){
		$id = I('id');
		if(tep_not_null($id) && is_numeric($id)){	
			$image = M("other")->where(array("other_id" => $id))->getField('image');
			@unlink("./Public/images/products/".$image);
			@unlink("./Public/images/products/thumb/s_".$image);
			@unlink("./Public/images/products/thumb/m_".$image);
			$file = M('other_images')->where(array('other_id'=> $id))->select();
			foreach($file as $v){
				@unlink("./Public/images/products/".$v['image']);
				@unlink("./Public/images/products/thumb/s_".$v['image']);
				@unlink("./Public/images/products/thumb/m_".$v['image']);
			}
			M('other')->where(array('other_id' => $id))->delete();
			M('other_images')->where(array('other_id' => $id))->delete();
			S('success','Products deleted successfully!');
			$this->redirect('index');
		}else{
			S('err','Products deleted failure!');
			$this->redirect('index');
			}
		
		
	}	

/*======================================================================
	刪除批量上傳的圖片
======================================================================================*/
	public function removeImg(){
		$file = $_GET['file'];
        @unlink("./Public/images/products/".$file);
		@unlink("./Public/images/products/thumb/m_".$file);
		@unlink("./Public/images/products/thumb/s_".$file);
	}
/*======================================================================
	刪除批量已保存到數據庫的圖片和數據
======================================================================================*/
	public function removePic(){
		$id = $_GET['id'];
		$file = M('products_images')->where(array('id'=> $id))->getField('image');
        @unlink("./Public/images/products/".$file);
		@unlink("./Public/images/products/thumb/m_".$file);
		@unlink("./Public/images/products/thumb/s_".$file);
		M('products_images')->where(array('id'=> $id))->delete();
	}
	public function ToExcel(){
			if((isset($_GET['sort'] ) && !empty($_GET['sort'])) && (isset($_GET['updown'] ) && !empty($_GET['updown']))){
			 $sort   = I('sort');
			 $upDown = I('updown');
		}else{
			 $_GET['sort'] = $sort = "other_id";
			 $_GET['updown'] = $upDown = "desc";
			
		}
		
		$keyword = I('keyword','');
		
		//關鍵字查詢
		if(tep_not_null($keyword)){
			$where['name']  = array('like','%'.$keyword.'%');
			$where['tel']  = array('like','%'.$keyword.'%');
			$where['_logic'] = 'or';
			$map['_complex'] = $where;
		}
		$begintime = I('begintime','');
		$endtime = I('endtime','');
		
		//echo $begintime;die;
		
		if($begintime){
			$begintime = date('Y-m-d H:i:s',strtotime($begintime));
			$map['add_date'] = array('egt',$begintime);			
			if($endtime){
				$endtime = date('Y-m-d H:i:s',strtotime($endtime));
				$map["add_date"] = array(array('EGT',$begintime),array('ELT',$endtime),'AND');
			}else{
				$endtime = date('Y-m-d H:i:s',time());
				$map["add_date"] = array(array('EGT',$begintime),array('ELT',$endtime),'AND');
			}	
		}elseif($endtime){			
				//$this->endtime = $endtime;
				$endtime = date('Y-m-d H:i:s',strtotime($endtime));
				$map['add_date'] = array('elt',$endtime);
		}
		
			
		//$map["add_date"] = array(array('EGT',$begintime),array('ELT',$endtime),'AND');
		//省份城市筛选
		$qid = I('citycode','');
		$aid = I('area_id','');
		
		if($qid > 0){
			$map['cityCode'] = $qid;
		}
		// 社区筛选
		if ($aid > 0) {
			$map['areaCode'] = $aid;
		}
		$map = get_map_city_id($map);
		//var_dump($map);die;
		
		$result = D('OtherView')->getAll($map,'',$sort.' '.$upDown);//getAll($map,$Page->firstRow. ',' . $Page->listRows,$sort.' '.$upDown);
		//var_dump($result);die;
		$xls_name = "商品列表";
		header("Content-type:application/vnd.ms-excel;charset=UTF-8");
		header("Content-Disposition:attachment;filename=$xls_name.xls");
	
		$table = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel"	xmlns="http://www.w3.org/TR/REC-html40">
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html>
            <head>
            <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
            <style id="Classeur1_16681_Styles"></style>
            </head>';
		$table .= '<table style="border:1px solid #000; border-spacing:0px; width:1000px;text-align:left;border-collapse: collapse;">';
		$table .= '<thead><tr style="background:#c6c6c6;height:40px;"><th style="border:1px solid #000;">编号</th><th style="border:1px solid #000;">姓名</th><th style="border:1px solid #000;">电话</th><th style="border:1px solid #000;">城市</th><th style="border:1px solid #000;">分区</th><th style="border:1px solid #000;">衣服(件)</th><th style="border:1px solid #000;">书籍(件)</th><th style="border:1px solid #000;">其他(件)</th><th style="border:1px solid #000;">添加日期</th></thead>';
		$table .= '<tbody>';
		foreach($result as $key=>$value){
			if(($key+1)%2==0){
				$table .= "<tr style='background:#a9c6c9'>";
			}else{
				$table .= "<tr>";
			}
	
			$table .= "<td style='border:1px solid #000;'>".$value['other_id']."</td>";
			$table .= "<td style='border:1px solid #000;'>".$value['name'].'</td>';
			$table .= "<td style='border:1px solid #000;'>".$value['tel'].'</td>';
			$table .= "<td style='border:1px solid #000;'>".$value['cname'].'</td>';
			$table .= "<td style='border:1px solid #000;'>".$value['aname'].'</td>';
			$table .= "<td style='border:1px solid #000;'>".$value['clothes_num'].'</td>';
			$table .= "<td style='border:1px solid #000;'>".$value['book_num'].'</td>';
			$table .= "<td style='border:1px solid #000;'>".$value['othercount'].'</td>';		
			$table .= "<td style='border:1px solid #000;'>".$value['add_date'].'</td>';
			$table .= '</tr>';
		}
		$table .= '</tbody>';
		$table .= '</table>';
		echo $table;
	}

}

?>
